/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.timesoft.base.active.ctrl;

import com.timesoft.common.bean.ResponseCommon;
import com.timesoft.common.convertor.Base64ConvertorUtils;
import com.timesoft.global.webservice.WebServiceClient;
import com.timesoft.hibernate.bean.Customer;
import com.timesoft.webservice.client.base.BaseWebService;
import com.timesoft.zk.common.ResourceUtil;
import com.timesoft.zk.common.ctrl.IncludePageWsController;
import com.timesoft.zk.common.event.MainEventWsListener;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Label;
import org.zkoss.zul.Timer;

/**
 *
 * @author sorasaks
 */
public class ActiveListCtrl extends IncludePageWsController {

    private static final Logger LOG = Logger.getLogger(ActiveListCtrl.class);

    private Timer timer;
    private Label lbTimer;
    private Label lbResult;
    private Integer count;

    private final BaseWebService baseService = WebServiceClient.getBaseService();
    private final Base64ConvertorUtils base64 = new Base64ConvertorUtils();

    public ActiveListCtrl() throws Exception {
        super();
    }

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp); //To change body of generated methods, choose Tools | Templates.
        initailComponent();
        initailEventListener();

        String customerCode = Executions.getCurrent().getParameter("u");
        if (StringUtils.isEmpty(customerCode)) {
            lbResult.setValue(ResourceUtil.getLabel("home.label.active.empty.link", locale));
            timer.start();
        } else {
            ResponseCommon common = (ResponseCommon) base64.stringToGenericObj(baseService.customerCoreGetCustomerByCustomerCode(customerCode,
                base64.genericObjToString(null)));
            Customer customer = (Customer) base64.stringToGenericObj(common.getValue());

            if (customer == null) {
                lbResult.setValue(ResourceUtil.getLabel("home.label.active.empty.link", locale));
                timer.start();
            } else {
                if (customer.isActive()) {
                    lbResult.setValue(ResourceUtil.getLabel("home.label.active.is.active", locale));
                    timer.start();
                } else {
                    customer.setActive(Boolean.TRUE);
                    common = (ResponseCommon) base64.stringToGenericObj(baseService.customerCoreUpdateActiveCustomer(base64.genericObjToString(customer),
                            base64.genericObjToString(Executions.getCurrent().getRemoteAddr())));

                    Boolean isUpdate = (Boolean) base64.stringToGenericObj(common.getValue());

                    if (isUpdate && common.isFlag()) {
                        lbResult.setValue(ResourceUtil.getLabel("home.label.active.success", locale));
                        timer.start();
                    } else {
                        lbResult.setValue(common.getMessage());
                        timer.start();
                    }
                }
            }
        }
    }

    private void initailComponent() {
        this.count = 10;
    }

    private void initailEventListener() throws Exception {
        timer.addEventListener(Events.ON_TIMER, new timerEventListener());
    }

    private class timerEventListener extends MainEventWsListener implements EventListener<Event> {

        public timerEventListener() throws Exception {
            super();
        }
        
        @Override
        public void onEvent(Event t) throws Exception {
            if (count == 0) {
                timer.stop();
                Executions.getCurrent().sendRedirect(contextPath);
                return;
            }
            lbTimer.setValue(ResourceUtil.getLabel("home.label.active.timer.before", locale)
                    + " "
                    + --count
                    + " "
                    + ResourceUtil.getLabel("home.label.active.timer.after", locale));
            LOG.debug("::::::::::::::::::::: " + lbTimer.getValue());
        }
    }
}
